package com.D5Power.SceneTrans
{
	import com.D5Power.D5Game;
	
	import fl.transitions.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	
	public class BlindsTrans implements ISceneTrans
	{
		private var game:D5Game;
		private var loader:Loader;
		private var trans:MovieClip;
		public function BlindsTrans(game:D5Game)
		{
			this.game = game;
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImgCompleteHandler);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onErrorHandler);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onErrorHandler);
			loader.load(new URLRequest("images/trans.jpg"));			
		}
		private function onErrorHandler(e:Event):void 
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadImgCompleteHandler);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onErrorHandler);
			loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onErrorHandler);			
			trace("Load translate image error.");
		}
		
		private function loadImgCompleteHandler(e:Event):void 
		{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadImgCompleteHandler);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onErrorHandler);
			loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onErrorHandler);			
			trans = new MovieClip();
			trans.addChild(loader);	
		}		
		
		public function start(callBack:Function):void
		{
			if (trans.parent == null)
				game.parent.addChild(trans);			
			var bd:BitmapData = new BitmapData(game.width, game.height);
			bd.draw(game);	
			var imgMC:MovieClip = new MovieClip();
			imgMC.addChild(new Bitmap(bd));	
			game.parent.addChild(imgMC);
			game.visible = true;
			TransitionManager.start(imgMC, { type:Blinds, direction:Transition.IN, duration:1 } );
			var timer:Timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, function(event:TimerEvent):void
			{
				timer.removeEventListener(TimerEvent.TIMER, arguments.callee);
				timer.stop();
				game.parent.removeChild(trans);
				game.parent.removeChild(imgMC);
				imgMC = null;
				bd.dispose();
				callBack();
			});
			timer.start();
		}
		
		public function end(callBack:Function):void
		{
			trans = new MovieClip();
			trans.addChild(loader);
			game.parent.addChild(trans);
			TransitionManager.start(trans, { type:Blinds, direction:Transition.IN, duration:1 } );
			var timer:Timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, function(event:TimerEvent):void
			{
				timer.removeEventListener(TimerEvent.TIMER, arguments.callee);
				timer.stop();
				callBack();
			});
			timer.start();			
		}
	}
}